<?php

namespace app\api\model\kuyou\applet;
use app\api\controller\kuyou\applet\Commons;
use think\Model;


class Settlementtype extends Model
{

    // 表名
    protected $name = 'kuyou_settlementtype';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';


    /**
     * 获取列表
     * @param array $where
     * @param int $page
     * @param int $limit
     * @return array
     */
    public static function getList($where,$page,$limit){
        $app_page = config('app_page');
        $app_page = $app_page===false?false:true;
        $pageStatus = true;
        if($app_page){
            if($page<0){
                $pageStatus = false;
            }
        }
        if($pageStatus){
            $list = collection(self::where(function ($query) use ($where) {
                $query->where($where);
            })->page($page,$limit)->field("id,name,image,status,FROM_UNIXTIME(createtime,'%Y-%m-%d') as createtime,FROM_UNIXTIME(updatetime,'%Y-%m-%d') as updatetime")->select())->toArray();
        }else{
            $list = collection(self::where(function ($query) use ($where) {
                $query->where($where);
            })->field("id,name,image,status,FROM_UNIXTIME(createtime,'%Y-%m-%d') as createtime,FROM_UNIXTIME(updatetime,'%Y-%m-%d') as updatetime")->select())->toArray();
        }

        $count = self::where(function ($query) use ($where) {
            $query->where($where);
        })->count();
        return ['list'=>$list,'count'=>$count];
    }

    /**
     * 获取详情
     * @param int $id
     * @return array
     */
    public static function getDetail($id){
        $detail = self::where(function ($query) use ($id) {
            $query->where('id', '=', $id);
        })->field('id,name,image,status')->find();
        return $detail;
    }

    /**
     * 添加
     * @param array $detail
     * @return array
     */
    public static function addDetail($detail){
        if(empty($detail)){
            return ['status'=>400,'msg'=>'参数错误'];
        }else{
            $check = (new Commons())->checkParameter($detail,[
                'name|名称'=>'require',
                'status|状态'=>'require|in:0,1',
            ]);
            if(isset($check['status'])){
                return $check;
            }else{
                $res = (new Settlementtype())->insertGetId(($detail));
                if($res){
                    return ['status'=>200,'msg'=>$res];
                }else{
                    return ['status'=>500,'msg'=>'添加失败'];
                }
            }
        }
    }

    /**
     * 更新
     * @param array $detail
     * @return array
     */
    public static function editDetail($detail){
        if(empty($detail)){
            return ['status'=>400,'msg'=>'参数错误'];
        }else{
            $check = (new Commons())->checkParameter($detail,[
                'id|ID'=>'require|number',
                'name|名称'=>'require',
                'status|状态'=>'require|in:0,1',
            ]);
            if(isset($check['status'])){
                return $check;
            }else{
                $id = $detail['id'];unset($detail['id']);
                $res = (new Settlementtype())->where(['id'=>$id])->update($detail);
                if($res){
                    return ['status'=>200,'msg'=>'更新成功'];
                }else{
                    return ['status'=>500,'msg'=>'更新失败'];
                }
            }
        }
    }

    /**
     * 删除
     * @param int $id
     * @return array
     */
    public static function delDetail($id){
        if(empty($id)){
            return ['status'=>400,'msg'=>'参数错误'];
        }else{
            $res = (new Settlementtype())->where(['id'=>['in',$id]])->delete();
            if($res){
                return ['status'=>200,'msg'=>$res];
            }else{
                return ['status'=>500,'msg'=>'删除失败'];
            }
        }
    }
}
